package se.webbzon.boltzmann.item;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class ItemGroup {
	
	// The singleton instance
	private static ItemGroup singleton;
	
	// A collection of all available items
	private final List<AbstractItem> items;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new item group. **/
	private ItemGroup() {
		items = new ArrayList<AbstractItem>();
	
		initializeItems();
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	/** Returns the singleton item group instance. **/
	public static ItemGroup instance() {
		if (singleton == null)
			singleton = new ItemGroup();
		return singleton;
	}
	
	/** Returns the item associated with the given id. **/
	public AbstractItem getItem(int itemId) {
		if (itemId >= items.size())
			return null;
		else
			return items.get(itemId);
	}
	
	/** Returns the item associated with the given name. **/
	public Collection<AbstractItem> getItems(String itemName) {
		List<AbstractItem> result = new ArrayList<AbstractItem>();
		for (AbstractItem item : items) {
			if (item.getName().equals(itemName))
				result.add(item);
		}
		return result;
	}
	
	/** Returns an unmodifiable collection of all the items in
	 * the item group. **/
	public Collection<AbstractItem> getItems() {
		return Collections.unmodifiableCollection(items);
	}
	
	/** Returns the id of the provided abstract item in this item group. **/
	public int getId(AbstractItem item) {
		int id = 0;
		for (AbstractItem it : items) {
			if (it.equals(item))
				return id;
			else
				id++;
		}
		return -1;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Adds the given item to the item group. **/
	private boolean addItem(AbstractItem item) {
		return items.add(item);
	}
	
	/** Initializes the content of the item group. **/
	private void initializeItems() {
		/** Name, description, texture set name**/
		addItem(new KeyItem("Green orb","","orb_01"));
		addItem(new KeyItem("Red orb","","orb_02"));
		addItem(new KeyItem("Blue orb","","orb_03"));
		addItem(new KeyItem("Empty pot","This pot is empty.","pot_01"));
		
		addItem(new RestorationItem("Apple", "A shiny green apple from the trees of Heron.", 10, "item_apple", 5, 0));
		addItem(new RestorationItem("Orange", "A perfectly spherical orange from the Pytha desert.", 15, "item_orange", 0, 5));
		addItem(new RestorationItem("Blue", "Some delicious blueberries.", 15, "item_blueberries", 3, 2));
		addItem(new RestorationItem("Banana", "It looks like a parabolic function. Or maybe a phone?", 35, "item_banana", 10, 10));
		addItem(new RestorationItem("Strawberry", "It seems to be bioengineered...", 25, "item_strawberry", 6, 4));
		addItem(new RestorationItem("Water", "Just plain water.", 5, "item_water", 1, 2));
		addItem(new RestorationItem("Milk", "Shake it and drink.", 20, "item_milk", 10, 5));
		
		addItem(new KeyItem("Lunch box","A thermos, a sandwich and jelly. Looks appetizing!","lunch_box_01"));
	}
}
